package com.dcjt.firehotcarpad.manager

import android.content.Context
import android.speech.tts.TextToSpeech
import android.speech.tts.UtteranceProgressListener
import android.util.Log
import com.aries.library.fast.util.ToastUtil
import java.util.*

class TtsManager {
    private var tts: TextToSpeech? = null
    companion object {
        private var instance: TtsManager? = null
            get() {
                if (field == null) {
                    field = TtsManager()
                }
                return field
            }

        fun get(): TtsManager {
            return instance!!
        }
    }


    fun initTts(context:Context) {
            tts = TextToSpeech(context, TextToSpeech.OnInitListener { status ->
                if (status == TextToSpeech.SUCCESS) {
                    val result: Int = tts!!.setLanguage(Locale.CHINA)
                    if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE && result != TextToSpeech.LANG_AVAILABLE) {
                        ToastUtil.show("TTS不支持")
                    }
                }
            })
            tts!!.setOnUtteranceProgressListener(object : UtteranceProgressListener() {
                override fun onStart(utteranceId: String) {
//                startNui = false;
                    Log.e(
                        "Message",
                        "onStart: -tts.setOnUtteranceProgressListener-"
                    )
                }

                override fun onDone(utteranceId: String) {
//                startNui = true;
                    Log.e(
                        "Message",
                        "onDone: -tts.setOnUtteranceProgressListener-"
                    )
                }

                override fun onError(utteranceId: String) {
//                startNui = true;
                    Log.e(
                        "Message",
                        "onError: -tts.setOnUtteranceProgressListener-"
                    )
                }
            })

    }

    fun stopTTs() {
        if (tts != null) {
            tts!!.shutdown() //关闭TTS
        }
    }

    fun speeckTTs(commandTips: String) {
        if (tts!= null){
            tts!!.setPitch(1.5f)
            val params =java.util.HashMap<String, String>()
            params[TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID] = "12345"
            tts!!.speak(commandTips, TextToSpeech.QUEUE_FLUSH, params)
        }

    }
}
